/*
 * UpdateParticipantsService.java
 *
 * Created on 15 March 2006, 18:37
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package web.services;

//import business.pojos.Meeting;
//import business.pojos.User;
import java.util.ArrayList;
import se392.ateam2006.meetingmgmt.meeting.MeetingEntity;
import se392.ateam2006.user.UserEntity;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import se392.ateam2006.resourcemgmt.participant.ParticipationEntity;
import web.util.BusinessFacadeDelegate;

/**
 *
 * @author Phil
 */
public class UpdateParticipantsService extends UpdateService{
    
    /**
     * Creates a new instance of UpdateParticipantsService 
     */
    public UpdateParticipantsService() {
    }
    public String executePreDispatch(HttpServletRequest request,
            ServletContext context)
            throws Exception {
        BusinessFacadeDelegate delegate = this.getDelegate(request.getSession());
        String muid = request.getParameter(Service.MEETING_ID);
        String newKeyParticipants = request.getParameter(Service.PARTICIPANTS_NEW_KEY);
        String newNonKeyParticipants = request.getParameter(Service.PARTICIPANTS_NEW_NONKEY);
        muid="sam";
        MeetingEntity meeting = delegate.getMeeting(muid);
        
        String[] splitKeys = new String[0];
        String[] splitNon = new String[0];
        int end =0;
        if(newKeyParticipants != null){
            splitKeys = newKeyParticipants.split(",");
            end = splitKeys.length;
        }
        if(newNonKeyParticipants != null){
            splitNon = newNonKeyParticipants.split(",");
        }
        //TODO don't understand this
//        UserEntity[] users = new UserEntity[0];
//                
//        if(newKeyParticipants.length() <= 1 && newNonKeyParticipants.length() > 1){
//            users = new UserEntity[splitNon.length];
//            end=0;
//        }else if(newNonKeyParticipants.length() <= 1 && newKeyParticipants.length() > 1){
//            users = new UserEntity[splitKeys.length];
//        }else if(newKeyParticipants.length() > 1 && newNonKeyParticipants.length() > 1){
//            users = new UserEntity[splitKeys.length + splitNon.length];
//        }
        splitKeys[0] = "ccf4@aber.ac.uk";
        ArrayList<ParticipationEntity> participants = new ArrayList<ParticipationEntity>();
        if(splitKeys[0].length() > 1){
            for(int i=0;i<splitKeys.length;i++){
                ParticipationEntity tmp = new ParticipationEntity(delegate.getUser(splitKeys[i]), meeting, true);           
                participants.add(tmp);
            }
        }
        if(splitNon[0].length() > 1){
            for(int i=0;i<splitNon.length;i++){            
                ParticipationEntity tmp = new ParticipationEntity(delegate.getUser(splitKeys[i]), meeting, false);
                participants.add(tmp);
            }
        }
        
        delegate.addParticipants(muid,participants);
        
        //now setup the page to invite.
        
        
        request.getSession().setAttribute(Service.ATTRIBUTE_PARTICIPANTS, participants);
        request.getSession().setAttribute(Service.ATTRIBUTE_MEETING, meeting);
        return "invite";
    }
    
    public void executePostDispatch(HttpServletRequest request,
            ServletContext context)
            throws Exception {
        request.getSession().setAttribute(Service.ATTRIBUTE_PARTICIPANTS, null);
        request.getSession().setAttribute(Service.ATTRIBUTE_MEETING, null);
    }
    
}
